package com.un.ebs.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.un.ebs.core.exception.BusinessException;
import com.un.ebs.product.domain.ProductClassTime;
import com.un.ebs.product.dto.ProductClassTimeAddDto;
import com.un.ebs.product.dto.ProductClassTimeUpdateDto;
import com.un.ebs.product.dto.ProductClassTimeVo;
import com.un.ebs.product.mapper.ProductClassTimeMapper;
import com.un.ebs.product.service.ProductClassTimeService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @email liubocs@163.com
 * @date 2020/5/7 10:23
 */
@Service
public class ProductClassTimeServiceImpl extends ServiceImpl<ProductClassTimeMapper, ProductClassTime> implements ProductClassTimeService {


    @Override
    public void add(ProductClassTimeAddDto req) {
        if (req.getEndHour() < req.getStartHour()) {
            throw new BusinessException("结束时刻必须晚上开始时刻。");
        }
        if (req.getEndHour().equals(req.getStartHour()) && (req.getEndMinute() < req.getStartMinute() || req.getEndMinute().equals(req.getStartMinute()))) {
            throw new BusinessException("结束时间必须晚上开始时间。");
        }
        ProductClassTime obj = new ProductClassTime();
        obj.setClassId(req.getClassId());
        obj.setStartHour(req.getStartHour());
        obj.setStartMinute(req.getStartMinute());
        obj.setEndHour(req.getEndHour());
        obj.setEndMinute(req.getEndMinute());
        obj.setIsDelete(false);
        obj.insert();
    }

    @Override
    public void udpate(ProductClassTimeUpdateDto req) {
        if (req.getEndHour() < req.getStartHour()) {
            throw new BusinessException("结束时刻必须晚上开始时刻。");
        }
        if (req.getEndHour().equals(req.getStartHour()) && (req.getEndMinute() < req.getStartMinute() || req.getEndMinute().equals(req.getStartMinute()))) {
            throw new BusinessException("结束时间必须晚上开始时间。");
        }
        ProductClassTime obj = getById(req.getId());
        obj.setClassId(req.getClassId());
        obj.setStartHour(req.getStartHour());
        obj.setStartMinute(req.getStartMinute());
        obj.setEndHour(req.getEndHour());
        obj.setEndMinute(req.getEndMinute());
        obj.updateById();
    }

    @Override
    public void delete(String id) {
        baseMapper.deleteById(id);
    }

    @Override
    public List<ProductClassTimeVo> query(String classId) {
        LambdaQueryWrapper<ProductClassTime> lq = Wrappers.lambdaQuery();
        lq.eq(ProductClassTime::getClassId, classId);
        lq.orderByAsc(ProductClassTime::getStartHour);
        lq.orderByAsc(ProductClassTime::getStartMinute);
        return baseMapper.selectList(lq).stream().map(s -> {
            ProductClassTimeVo obj = new ProductClassTimeVo();
            BeanUtils.copyProperties(s, obj);
            return obj;
        }).collect(Collectors.toList());
    }
}
